<?php
header("Content-Type:text/html;charset=utf-8");

function Check_Name($username)
{
	$Max_Strlen_Name=16;
	$Min_Strlen_Pass=6;
	if(strlen($uesrname)>$Max_Strlen_Name || strlen($username)<$Min_Strlen_Name)
	{
		return false;
	}
	return true;
}

function Check_Pass($password)
{
	$Max_Strlen_Pass=16;
	$Min_Strlen_Pass=6;
	if( strlen($password)>$Max_Strlen_Pass || strlen($password)<$Min_Strlen_Pass )
	{
		return false;
	}
	return true;
}

function auth($username, $password)
{
	include_once("conn.php");
	$query1="select * from tb_user_info where name='".mysql_escape_string($username)."'";
	$result1=mysql_query($query1);
	$gotName=mysql_fetch_array($result1);

	if( $gotName["name"] == $username && $gotName["password"] == $password )
	{
		return true;
	}
	else
	{
		return false;
	}
}

$user1=$_POST["name2"];
$pass1=$_POST["pass2"];

$auth_result = false;
if (!Check_Name($user1))
{
	echo "用户名格式错误";
	exit;
}
else if (!Check_Pass($pass1))
{
	echo "密码格式错误";
	exit;
}
else if (auth($user1, $pass1))
{
   $auth_result = true;
   session_start();
   $_SESSION["current_user"] = $user1;
   setcookie("username", $user1, time()+86400, "/", "localhost");
   $arr['u1']=$_SESSION["current_user"];
   echo json_encode($arr);
}
else if(!auth($user1,$pass1))
{
	echo "用户名或密码错误或不存在";
}


?>